﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Unread.App.ViewModels;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Unread.App.Common;

namespace Unread.App.Views
{
    public sealed partial class UnreadPage : Unread.App.Common.LayoutAwarePage
    {
        public UnreadViewModel ViewModel
        {
            get
            {
                return DataContext as UnreadViewModel;
            }
        }

        public UnreadPage()
        {
            this.InitializeComponent();

            DataContext = ServicesFactory.GetInstance<UnreadViewModel>();
        }

        private void BottomAppBar1_Loaded(object sender, RoutedEventArgs e)
        {
            RegisterAppBarButtons();
        }

        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (HasFeedsInState(pageState))
                LoadFeedsFromState(pageState);
            else
                await ViewModel.Load();
        }

        private bool HasFeedsInState(Dictionary<string, object> pageState)
        {
            if (pageState == null) return false;

            return (pageState["Feeds"] as ObservableCollection<UnreadFeedViewModel>) != null;
        }

        private void LoadFeedsFromState(Dictionary<String, Object> pageState)
        {
            var savedFeeds = (pageState["Feeds"] as ObservableCollection<UnreadFeedViewModel>);

            foreach (var feed in savedFeeds)
            {
                ViewModel.Feeds.Add(feed);
            }

            RestoreScrollPosition(pageState);
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            pageState["Feeds"] = ViewModel.Feeds;

            var scrollviewer = itemGridView.GetFirstChild<ScrollViewer>();
            pageState["HorizontalOffset"] = scrollviewer.HorizontalOffset;
        }

        private void RegisterAppBarButtons()
        {
            Grid foo = BottomAppBar.Content as Grid;
            foreach (UIElement e in foo.Children)
            {
                foreach (UIElement c in ((StackPanel)e).Children)
                {
                    if (c.GetType() == typeof(Button))
                    {
                        base.StartLayoutUpdates(c, new RoutedEventArgs());
                    }
                }
            }
        }

        private void RestoreScrollPosition(Dictionary<String, Object> pageState)
        {
            var savedScrollPosition = pageState["HorizontalOffset"];
            if (savedScrollPosition != null)
            {
                itemGridView.Loaded += (s, e) =>
                {
                    var scrollviewer = itemGridView.GetFirstChild<ScrollViewer>();
                    scrollviewer.ScrollToHorizontalOffset((double)savedScrollPosition);
                };
            }
        }
    }
}
